package com.wecheir.service.impl;

import com.wecheir.dao.MessageDao;
import com.wecheir.entity.Message;
import com.wecheir.entity.User;
import com.wecheir.service.MessageService;
import com.wecheir.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Exception.class)
public class MessageServiceImpl implements MessageService {

    @Resource
    UserService userService;
    
    @Resource
    MessageDao messageDao;
    
    /**
     * 获得同一农田名字的数目
     * @param name
     * @return
     */
    @Override
    public long getMessageNameCount(String name) {
//        QueryWrapper<Message> wrapper = new QueryWrapper<>();
//        wrapper.eq("name",name);
//        return messageDao.selectCount(wrapper);
        return 1;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMessageSystem(String errorFlag) {
//        Message message=new Message();
//        message.setLooked("未读");
//        message.setTitle("传感器故障");
//        message.setContent(errorFlag);
//        message.setMessageType("系统报警");
//        User currentUser=userService.findUserById(MySysUser.id());
//        message.setCreateId(MySysUser.id());
//        message.setCreateDate(new Date());
//        message.setUpdateDate(new Date());
//        message.setUpdateId(MySysUser.id());
//        message.setCreateName("农田检测系统");
//        message.setToUser(currentUser.getId());
//        messageDao.insert(message);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message saveMessage(Message message) {
//        message.setLooked("未读");
//        User toUserEn=userService.findUserById(message.getToUser());
//        message.setCreateId(uid);
//        message.setCreateDate(new Date());
//        message.setUpdateDate(new Date());
//        message.setUpdateId(uid);
//        message.setCreateName(toUserEn.getNick_name());
//        message.setToUser(message.getToUser());
//        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
//        messageDao.saveMessageByUpdatePro(message,uuid);
//        return message;
        message.setLooked("未读");
        //发送和创建为同一个人
        message.setUpdateId(message.getCreateId());
        message.setCreateDate(new Date());
        message.setUpdateDate(new Date());

        User createUser = userService.findUserById(message.getCreateId());
        message.setCreateName(createUser.getNick_name());

        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        messageDao.saveMessageByUpdatePro(message,uuid);
        return message;
    }


    @Override
    public Message getMessageById(String id) {
        return messageDao.selectMessageById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMessage(Message message) {
        messageDao.updateById(message);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMessage(Message message) {
        message.setDelFlag(true);
        messageDao.updateById(message);
    }

    @Override
    public List<Message> selectAll() {
//        QueryWrapper wrapper = new QueryWrapper();
////        wrapper.eq("del_flag",false);
//        return messageDao.selectList(wrapper);
        return new ArrayList<>();
    }

    @Override
    public List<Message> selectMessageList(String toUser, Integer start, Integer limit) {
        return messageDao.selectMessageList(toUser, start, limit);
    }

    @Override
    public List<Message> selectAllByToUser(String toUser) {
        return messageDao.selectMessageList(toUser, 0, 999);
    }

    @Override
    public int selectMessageListCount(String toUser) {
        return messageDao.selectMessageListCount(toUser);
    }
}
